package com.jingtuo.android.page;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup;

import com.jingtuo.android.R;
import com.jingtuo.android.common.app.BaseActivity;
import com.jingtuo.android.common.os.BitmapInfo;
import com.jingtuo.android.common.widget.ViewPagerAdapter;
import com.jingtuo.android.fragment.PicturePreviewFragment;

public class PicturePreviewPage extends BaseActivity implements OnPageChangeListener{
	
	/**
	 * 传递BitmapInfo
	 */
	public static final String EXTRA_PICTURES = "EXTRA_PICTURES";
	
	public static final String EXTRA_CURRENT_INDEX = "EXTRA_CURRENT_INDEX";
	
	private ArrayList<BitmapInfo> bitmapInfos;
	
	private int currentIndex;
	
	private ActionBar actionBar;
	
	private ViewPager mViewPager;
	
	private Adapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.picture_preview);
		initView();
		getData();
	}
	
	@Override
	public void initView() {
		// TODO Auto-generated method stub
		super.initView();
		actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		mViewPager = (ViewPager)findViewById(R.id.picture_preview_viewpager);
	}
	
	@Override
	public void getData() {
		// TODO Auto-generated method stub
		super.getData();
		Intent intent = getIntent();
		bitmapInfos = intent.getParcelableArrayListExtra(EXTRA_PICTURES);
		currentIndex = intent.getIntExtra(EXTRA_CURRENT_INDEX, 0);
		if(bitmapInfos==null||bitmapInfos.size()<=0){
			finish();
		}
		if(currentIndex<0||currentIndex>bitmapInfos.size()-1){
			currentIndex = 0;
		}
		int screenWidth = getScreenWidth();
		for (int i = 0; i < bitmapInfos.size(); i++) {
			bitmapInfos.get(i).setIcon(false);
			bitmapInfos.get(i).setDstWidth(screenWidth);
		}
		adapter = new Adapter(getFragmentManager(), bitmapInfos);
		mViewPager.setAdapter(adapter);
		mViewPager.setCurrentItem(currentIndex);
		mViewPager.setOnPageChangeListener(this);
	}
	
	
	class Adapter extends ViewPagerAdapter<BitmapInfo> {

		public Adapter(FragmentManager fm, List<BitmapInfo> data) {
			super(fm, data);
			// TODO Auto-generated constructor stub
		}
		
		
		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			PicturePreviewFragment picturePreviewFragment = new PicturePreviewFragment();
			Bundle args = new Bundle();
			args.putParcelable(PicturePreviewFragment.EXTRA_PICTURE, data.get(position));
			picturePreviewFragment.setArguments(args);
			return picturePreviewFragment;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			super.destroyItem(container, position, object);
		}
		
		
		 @Override
		public String createTagName(int position) {
			// TODO Auto-generated method stub
			return data.get(position).getPath();
		}
		
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		currentIndex = arg0;
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.picture_preivew_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getItemId()==R.id.picture_preview_menu_delete){
			bitmapInfos.remove(currentIndex);
			if(bitmapInfos.size()==1){//移除最后一张,无预览图片离开
				finish();
			}else{
				if(currentIndex>0){
					currentIndex--;
				}
				adapter = new Adapter(getFragmentManager(), bitmapInfos);
				mViewPager.setAdapter(adapter);
				mViewPager.setCurrentItem(currentIndex);
			}
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
